﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Bll.CommonBll;
using CSP.Bll.IObjectBlls;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Common.Factories;
using CSP.Dao;
using CSP.Model.CSP;
using System.Collections.ObjectModel;

namespace CSP.Bll.ObjectBlls
{
    public class PriceUnitBll : CSPCommonBll, IPriceUnitBll
    {
        /// <summary>
        /// 缓存键值
        /// </summary>
        private const string CACHEKEYALL = "AgeGroupAll";

        private IPriceUnitDao _priceUnitDao;

        public PriceUnitBll()
        {
            _cacheProvider = CachedProviderFactory.GetLocalCachedProvider();
            _priceUnitDao = CSPDaoFactory.GetInstance().GetPriceUnitDao();
        }

        public ICollection<Model.CSP.PriceUnit> FindAll()
        {
            //检测缓存是否存在
            ICollection<PriceUnit> priceUnits = _cacheProvider.GetData<PriceUnit[]>(CACHEKEYALL);
            if (priceUnits == null)
            {
                //从数据库读取数据
                priceUnits = _priceUnitDao.FindAll();
                _cacheProvider.AddData(CACHEKEYALL, priceUnits, DateTime.Now.AddHours(10));
            }
            return priceUnits;
        }

        public Model.CSP.PriceUnit GetDataById(int id)
        {
            PriceUnit priceUnit = GetById(id);
            if (priceUnit == null)
            {
                FindAll();
                priceUnit = GetById(id);
            }

            return priceUnit;
        }

        public ICollection<PriceUnit> GeDatatByIds(int[] Ids)
        {
            ICollection<PriceUnit> priceUnits = GetByIds(Ids);
            if (priceUnits == null || priceUnits.Count == 0)
            {
                FindAll();
                priceUnits = GetByIds(Ids);
            }

            return priceUnits; 
        }

        public void Add(Model.CSP.PriceUnit obj)
        {
            if (obj == null)
            {
                throw new Exception("PriceUnit Can't be null");
            }
            _priceUnitDao.Add(obj);

            //清除缓存
            this.RefreshCache();
        }

        public void Update(Model.CSP.PriceUnit obj)
        {
            if (obj == null)
            {
                throw new Exception("PriceUnit Can't be null");
            }
            _priceUnitDao.Update(obj);

            //清除缓存0
            this.RefreshCache();
        }

        public void DeleteById(int id)
        {
            _priceUnitDao.DeleteById(id);

            //清除缓存0
            this.RefreshCache();
        }

        public void RefreshCache()
        {
            //删除缓存Key
            _cacheProvider.Remove(CACHEKEYALL);
        }

        /// <summary>
        /// 通过Id 获取年龄范围
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private PriceUnit GetById(int id)
        {
            PriceUnit priceUnit = null;

            //检测缓存是否存在
            ICollection<PriceUnit> priceUnits = GetByIds(new int[] { id });
            if (priceUnits != null && priceUnits.Count != 0)
            {
                priceUnit = priceUnits.FirstOrDefault();
            }
            return priceUnit;
        }

        private ICollection<PriceUnit> GetByIds(int[] ids)
        {
            ICollection<PriceUnit> result = new Collection<PriceUnit>();

            //检测缓存是否存在
            ICollection<PriceUnit> priceUnits = _cacheProvider.GetData<Collection<PriceUnit>>(CACHEKEYALL);
            foreach (var item in priceUnits)
            {
                if (ids.Contains(item.id))
                {
                    result.Add(item);
                }
            }
            return result;
        }
    }
}
